package com.example.petstore.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.petstore.common.CommonResponse;
import com.example.petstore.domain.Category;
import com.example.petstore.domain.Product;
import com.example.petstore.mapper.ProductMapper;
import com.example.petstore.util.OSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.net.URI;

import static com.example.petstore.util.OSSUtil.upload;

@Service("productService")
public class ProductService {

    @Autowired
    private ProductMapper productMapper;
    public CommonResponse getProductList() {
        List<Product> productList = productMapper.selectList(null);
        return CommonResponse.creatForSuccess(productList);
    }
    public CommonResponse getProductIdsByCategory() {
        Map<String, List<String>> categoryProductIdsMap = new HashMap<>();

        List<Product> productList = productMapper.selectList(null);

        for (Product product : productList) {
            String category = product.getCategory();
            String productId = product.getProductid();

            if (categoryProductIdsMap.containsKey(category)) {
                categoryProductIdsMap.get(category).add(productId);
            } else {
                List<String> productIdList = new ArrayList<>();
                productIdList.add(productId);
                categoryProductIdsMap.put(category, productIdList);
            }
        }

        return CommonResponse.creatForSuccess(categoryProductIdsMap);
    }

    public CommonResponse<List<Product>> getProductListByCategory(String category) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("category", category);
        List<Product> productList = productMapper.selectList(queryWrapper);
        System.out.println(productList);
        return CommonResponse.creatForSuccess(productList);
    }

    public CommonResponse getProductListByKey(String keyWord) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", keyWord);
        List<Product> productList = productMapper.selectList(queryWrapper);
        System.out.println(productList);
        return CommonResponse.creatForSuccess(productList);
    }
    public CommonResponse<Product> addProduct(Product product) {

        int res = productMapper.insert(product);
        if(res > 0) {
            return CommonResponse.creatForSuccess(product);
        }
        else {
            return CommonResponse.creatForError();
        }
    }

    public String handleFileUpload(MultipartFile file,String name) {
        String originalFileName = file.getOriginalFilename();
        String fileExtension = originalFileName.substring(originalFileName.lastIndexOf('.'));
        String fileName = name + fileExtension; // 新文件名
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
            OSSUtil.upload("petstore/" + fileName, inputStream);

            String filePath = "https://pet-store-w.oss-cn-beijing.aliyuncs.com/petstore/"+fileName;
            System.out.println(filePath);
            return filePath;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }


//        try (InputStream inputStream = file.getInputStream()) {
//            Files.copy(inputStream, Paths.get(uploadDir, fileName), StandardCopyOption.REPLACE_EXISTING);
//
//            System.out.println(filePath);
//            return filePath;
//        } catch (IOException e) {
//            e.printStackTrace();
//
//        }
    }

    public List<String> getProductIdList() {
        List<Product> productList = productMapper.selectList(null);
        List<String> productIdList = new ArrayList<>();
    //    System.out.println(productList);
        for (Product product : productList) {
            productIdList.add(product.getProductid());
        }

        return productIdList;
    }
    public Product getProductById(String productId) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productid", productId);
        Product product = productMapper.selectOne(queryWrapper);
        return product;
    }
    public CommonResponse deleteProductById(String productId) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productid", productId);
        int res = productMapper.delete(queryWrapper);
        if(res > 0) {
            return CommonResponse.creatForSuccess();
        }
        else {
            return CommonResponse.creatForError();
        }
    }
    public CommonResponse updateProductByProductId(String productId, Product newProduct) {
        UpdateWrapper<Product> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("productid", productId);

        int res = productMapper.update(newProduct, updateWrapper);
        if(res==1) {
            return CommonResponse.creatForSuccess();
        }
        else {
            return CommonResponse.creatForError();
        }
    }
}
